package com.g1.controller;

import com.g1.entity.Cook;
import com.g1.entity.Teacher;
import com.g1.mapper.TeacherMapper;
import com.g1.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;


    @RequestMapping("selectAll")
    public String selectAll(HttpServletRequest request){

        List<Teacher> teacher = teacherService.selectAll();
        request.setAttribute("teacher",teacher);
        return "TeacherList";
    }

    @RequestMapping("selectById")
    public String  selectById(@RequestParam("id") Long id, HttpServletRequest request){
        Teacher teacher = teacherService.selectById(id);
        request.setAttribute("teacher",teacher);
        return "updateTeacher";
    }

    @RequestMapping("update")
    public String update(Teacher teacher){
        int update = teacherService.update(teacher);
        return "redirect:/teacher/selectAll";
    };



    @RequestMapping("delete")
    public String delete(@RequestParam("id") Long id){
        int update = teacherService.delete(id);
        return "redirect:/teacher/selectAll";
    };


    @RequestMapping("add")
    public String add(Teacher teacher){
        int update = teacherService.insert(teacher);
        return "redirect:/teacher/selectAll";
    };

    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }


}
